package com.mkz.jbpm.controller;

import java.util.List;

import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mkz.core.common.util.ResponseResult;
import com.mkz.core.log.annotation.SysLog;
import com.mkz.core.security.util.SecurityUtils;
import com.mkz.jbpm.activiti.ActTaskService;
import com.mkz.jbpm.entity.AuditTask;
import com.mkz.jbpm.service.AuditTaskService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;


/**
 * 待办任务
 *
 * @author KuangJianyu
 * @date 2020-04-05 07:59:56
 */
@RestController
@AllArgsConstructor
@RequestMapping("/audittask" )
@Api(value = "audittask", tags = "待办任务管理")
public class AuditTaskController {

    private final  AuditTaskService auditTaskService;
    private final ActTaskService actTaskService;

    /**
     * 分页查询
     * @param page 分页对象
     * @param auditTask 待办任务
     * @return
     */
    @ApiOperation(value = "分页查询", notes = "分页查询")
    @GetMapping("/page" )
    public ResponseResult getAuditTaskPage(Page page, AuditTask auditTask) {
    	List<AuditTask> list = auditTaskService.findTaskByUsername(SecurityUtils.getUser().getUsername());
		IPage<AuditTask> tpage = new Page<AuditTask>(page.getCurrent(), page.getSize());
		tpage.setRecords(list);
        return ResponseResult.ok(tpage);
    }


    /**
     * 通过id查询待办任务
     * @param id id
     * @return ResponseResult
     */
    @ApiOperation(value = "通过id查询", notes = "通过id查询")
    @GetMapping("/{id}" )
    public ResponseResult getById(@PathVariable("id" ) String id) {
        return ResponseResult.ok(auditTaskService.getById(id));
    }

    /**
     * 领取任务
     * @param id 任务主键
     * @return ResponseResult
     */
    @ApiOperation(value = "领取任务", notes = "领取任务")
    @SysLog("领取任务" )
    @PostMapping("/claim")
    @PreAuthorize("@pms.hasPermission('jbpm_audittask_claim')" )
    public ResponseResult<Boolean> agree(String id) {
    	try {
			actTaskService.claim(id, SecurityUtils.getUser().getUsername());
		} catch (Exception e) {
			e.printStackTrace();
			return ResponseResult.failed("处理失败");
		}
        return ResponseResult.ok(true);
    }

    /**
     * 待办任务处理-同意
     * @param id 任务主键
     * @param remark 备注
     * @return ResponseResult
     */
    @ApiOperation(value = "同意", notes = "推送到下个节点")
    @SysLog("同意" )
    @PostMapping("/agree")
    @PreAuthorize("@pms.hasPermission('jbpm_audittask_agree')" )
    public ResponseResult<Boolean> agree(String id, String remark) {
    	boolean dealResult = actTaskService.completeAndDealNext(id, null, remark);
    	if(!dealResult) {
    		return ResponseResult.failed("处理失败");
    	}
        return ResponseResult.ok(true);
    }

    /**
     * 待办任务处理-不同意
     * @param id 任务主键
     * @param remark 备注
     * @return ResponseResult
     */
    @ApiOperation(value = "同意", notes = "推送到下个节点")
    @SysLog("同意" )
    @PostMapping("/disagree")
    @PreAuthorize("@pms.hasPermission('jbpm_audittask_disagree')" )
    public ResponseResult<Boolean> disagree(String id, String remark) {
    	boolean dealResult = true;//actTaskService.completeAndDealNext(id, null, remark);
    	if(!dealResult) {
    		return ResponseResult.failed("处理失败");
    	}
        return ResponseResult.ok(true);
    }

    /**
     * 修改待办任务
     * @param auditTask 待办任务
     * @return ResponseResult
     */
    @ApiOperation(value = "修改待办任务", notes = "修改待办任务")
    @SysLog("修改待办任务" )
    @PutMapping
    @PreAuthorize("@pms.hasPermission('jbpm_audittask_edit')" )
    public ResponseResult updateById(@RequestBody AuditTask auditTask) {
        return ResponseResult.ok(auditTaskService.updateById(auditTask));
    }

    /**
     * 通过id删除待办任务
     * @param id id
     * @return ResponseResult
     */
    @ApiOperation(value = "通过id删除待办任务", notes = "通过id删除待办任务")
    @SysLog("通过id删除待办任务" )
    @DeleteMapping("/{id}" )
    @PreAuthorize("@pms.hasPermission('jbpm_audittask_del')" )
    public ResponseResult removeById(@PathVariable String id) {
        return ResponseResult.ok(auditTaskService.removeById(id));
    }

}
